// pages/volunteer/volunteer-detail.js
Page({
  data: {
    userInfo: {
      name: '晴天',
      volunteerId: '123444890',
      totalPoints: 109346,
      redeemedPoints: 200,
      currentPoints: 185
    },
    taskList: [
      {
        id: 1,
        title: '参与活动，上传现场照片',
        description: '参与志愿活动并上传现场照片记录',
        points: 50,
        status: 'pending',
        actionText: '去上传'
      },
      {
        id: 2,
        title: '发布推文',
        description: '在社交媒体分享志愿活动心得',
        points: 30,
        status: 'pending',
        actionText: '去发布'
      },
      {
        id: 3,
        title: '每日登录',
        description: '每日登录小程序获得积分',
        points: 5,
        status: 'completed',
        actionText: '已登录'
      },
      {
        id: 4,
        title: '推荐志愿者',
        description: '成功推荐新志愿者加入',
        points: 100,
        status: 'pending',
        actionText: '去推荐'
      }
    ]
  },

  onLoad: function (options) {
    console.log('志愿者详情页面加载');
    this.loadUserData();
  },

  onShow: function () {
    // 页面显示时刷新数据
    this.refreshData();
  },

  // 加载用户数据
  loadUserData: function () {
    // 从本地存储或服务器获取用户数据
    const userData = wx.getStorageSync('volunteerUserData');
    if (userData) {
      this.setData({
        userInfo: { ...this.data.userInfo, ...userData }
      });
    }
  },

  // 刷新数据
  refreshData: function () {
    // 刷新积分和任务状态
    this.loadUserData();
    this.updateTaskStatus();
  },

  // 更新任务状态
  updateTaskStatus: function () {
    // 检查每日登录状态
    const today = new Date().toDateString();
    const lastLogin = wx.getStorageSync('lastLoginDate');
    
    const taskList = this.data.taskList.map(task => {
      if (task.id === 3) { // 每日登录任务
        if (lastLogin === today) {
          return { ...task, status: 'completed', actionText: '已登录' };
        } else {
          return { ...task, status: 'pending', actionText: '去登录' };
        }
      }
      return task;
    });
    
    this.setData({ taskList });
  },

  // 处理任务
  handleTask: function (e) {
    const taskId = e.currentTarget.dataset.id;
    const task = this.data.taskList.find(t => t.id === taskId);
    
    if (!task || task.status === 'completed') return;
    
    switch (taskId) {
      case 1: // 上传照片
        this.uploadPhoto();
        break;
      case 2: // 发布推文
        this.publishPost();
        break;
      case 3: // 每日登录
        this.dailyLogin();
        break;
      case 4: // 推荐志愿者
        this.recommendVolunteer();
        break;
    }
  },

  // 上传照片
  uploadPhoto: function () {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        wx.showLoading({ title: '上传中...' });
        
        // 模拟上传
        setTimeout(() => {
          wx.hideLoading();
          this.completeTask(1, 50);
          wx.showToast({
            title: '上传成功 +50分',
            icon: 'success'
          });
        }, 2000);
      }
    });
  },

  // 发布推文
  publishPost: function () {
    wx.navigateTo({
      url: '/pages/volunteer/publish'
    });
  },

  // 每日登录
  dailyLogin: function () {
    // 检查是否已经登录过
    const today = new Date().toDateString();
    const lastLogin = wx.getStorageSync('lastLoginDate');
    
    if (lastLogin === today) {
      wx.showToast({
        title: '今日已登录',
        icon: 'none'
      });
      return;
    }
    
    // 记录登录并给予积分
    wx.setStorageSync('lastLoginDate', today);
    this.completeTask(3, 5);
    
    wx.showToast({
      title: '登录成功 +5分',
      icon: 'success'
    });
  },

  // 推荐志愿者
  recommendVolunteer: function () {
    wx.navigateTo({
      url: '/pages/volunteer/recommend'
    });
  },

  // 完成任务
  completeTask: function (taskId, points) {
    const taskList = this.data.taskList.map(task => {
      if (task.id === taskId) {
        return {
          ...task,
          status: 'completed',
          actionText: '已完成'
        };
      }
      return task;
    });
    
    const userInfo = { ...this.data.userInfo };
    userInfo.currentPoints += points;
    userInfo.totalPoints += points;
    
    this.setData({
      taskList,
      userInfo
    });
    
    // 保存到本地存储
    wx.setStorageSync('volunteerUserData', userInfo);
  },

  // 返回上一页
  goBack: function () {
    wx.navigateBack();
  },

  // 跳转到签到
  goToCheckin: function () {
    wx.navigateTo({
      url: '/pages/volunteer/checkin'
    });
  },

  // 跳转到活动列表
  goToActivities: function () {
    wx.navigateTo({
      url: '/pages/volunteer/activities'
    });
  },

  // 跳转到积分商城
  goToMall: function () {
    wx.navigateTo({
      url: '/pages/volunteer/mall'
    });
  },

  // 跳转到排行榜
  goToRanking: function () {
    wx.navigateTo({
      url: '/pages/volunteer/ranking'
    });
  },

  // 跳转到消息中心
  goToMessages: function () {
    wx.navigateTo({
      url: '/pages/volunteer/messages'
    });
  },

  // 跳转到个人资料
  goToProfile: function () {
    wx.navigateTo({
      url: '/pages/volunteer/profile'
    });
  },

  // 跳转到积分明细
  goToPointsDetail: function () {
    wx.navigateTo({
      url: '/pages/volunteer/points'
    });
  },

  // 跳转到地址管理
  goToAddress: function () {
    wx.navigateTo({
      url: '/pages/volunteer/address'
    });
  },

  // 跳转到订单管理
  goToOrders: function () {
    wx.navigateTo({
      url: '/pages/volunteer/orders'
    });
  },

  // 分享功能
  onShareAppMessage: function () {
    return {
      title: '志愿者中心 - 积分任务与功能',
      path: '/pages/volunteer/volunteer-detail',
      imageUrl: '/images/share-volunteer-detail.png'
    };
  },

  // 分享到朋友圈
  onShareTimeline: function () {
    return {
      title: '志愿者中心 - 积分任务与功能',
      imageUrl: '/images/share-volunteer-detail.png'
    };
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  }
});
